package com.jwthhealth_pub.old.individual.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.constitution.ConstitutionTypeUtil;
import com.jwthhealth_pub.old.constitution.view.ConstitutionActivity;
import com.jwthhealth_pub.old.individual.module.JztzModule;
import com.jwthhealth_pub.old.individual.view.widget.EvaluateChart;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 个人中心 九种体制结果
 * Created by Administrator on 2017/1/17.
 */
public class IndividualEvaluateActivity extends Activity {
    @BindView(R.id.response_title)
    TextView responseTitle;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    @BindView(R.id.evaluate_chart)
    EvaluateChart mChart;
    @BindView(R.id.btn_retext)
    Button btnRetext;
    //    @BindView(R.id.chart_bar)
//    BarChart chartBar;
    @BindArray(R.array.constitution)
    String[] constitutions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indivudual_evaluate);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        initTop();
        initData();
    }

    private void initData() {
        LoginModel.DataBean dataBean = UserInfoDao.getInstance().queryUserinfoModel();
        if (dataBean == null) {
            return;
        }
        Call<JztzModule> jztiModule = ApiHelper.getJztiModule(dataBean.getId(), dataBean.getAndroidtoken());
        jztiModule.enqueue(new Callback<JztzModule>() {
            @Override
            public void onResponse(Call<JztzModule> call, Response<JztzModule> response) {
                final JztzModule body = response.body();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (body.getCode().equals("0")) {
                            List<JztzModule.DataBean> list = body.getData();
//                            List<Integer> datas = new ArrayList<>();
                            int[] datas = new int[9];
                            String[] names = new String[9];
//                            for (int i = 0; i < 9; i++) {
//                                datas.add(i, 0);
//                            }

//                            if (list.size() == 9) {
//                                responseTitle.setText("您的体质" + list.get(0).getResult_desc() + "平和质");
//                            }
                            if (body.getMsg() != null)
                                responseTitle.setText(body.getMsg());

                            for (int i = 0; i < list.size(); i++) {
                                JztzModule.DataBean dataBean1 = list.get(i);
                                String qid = dataBean1.getQid();
                                int constitutionIndex = ConstitutionTypeUtil.getConstitutionIndex(qid);
                                int e = (int) Float.parseFloat(dataBean1.getResult());
//                                datas.set(constitutionIndex, e);

                                datas[i] = e;
                                if (dataBean1.getName() != null)
                                    names[i] = dataBean1.getName();
                                else
                                    names[i] = "";
                            }
                            // TODO: 2017/12/4
                            mChart.setAxisValues(names, datas);
//                            green.start(2);
                        } else {
                            Toast.makeText(IndividualEvaluateActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                        }

//                        initCharBar(null);
                    }
                });
            }

            @Override
            public void onFailure(Call<JztzModule> call, Throwable t) {
                Log.d("IndividualEvaluateActiv", "error");
            }
        });

        boolean isRetest = getIntent().getBooleanExtra(Constant.ISRETEXT, false);
        if (isRetest)
            btnRetext.setVisibility(View.VISIBLE);
    }

//    private void initCharBar(List<Integer> datas) {
//
//        List<BarEntry> entries = new ArrayList<>();
//        Entry entry = new Entry();
//        for (int i = 0; i < constitutions.length; i++) {
//            if (datas != null) {
//                entries.add(new BarEntry(i, datas.get(i)));
//            } else {
//                entries.add(new BarEntry(i, 0));
//            }
////            entries2.add(new BarEntry(i,10 * i));
//        }
//
//        //创建数据集
//        BarDataSet dataSet = new BarDataSet(entries, ""); // add entries to dataset
////        dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
//
////        dataSet.setColor(R.color.primarypurple);
////        dataSet.setHighLightAlpha(255);
////        dataSet.setDrawValues(true);
//        dataSet.setStackLabels(null);
//        dataSet.setHighlightEnabled(false);
//        dataSet.setDrawValues(false);
//        List<IBarDataSet> dataSets=new ArrayList<>();
//        dataSets.add(dataSet);
//        dataSet.setDrawValues(true);
//        //柱状图数据集
//        BarData data = new BarData(dataSets);
//        //设置柱子宽度
//        data.setBarWidth(0.7f);
//        //隐藏左下角说明
//
//        XAxis xAxis = chartBar.getXAxis();
//        xAxis.setDrawGridLines(false);
//        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
////        xAxis.setAxisLineWidth(2f);
//        xAxis.setAxisLineColor(R.color.primarycolor);
//        xAxis.setTextSize(5);
//        xAxis.setTextColor(R.color.textColorBlack);
//
//        IAxisValueFormatter formatter = new IAxisValueFormatter() {
//
//            @Override
//            public String getFormattedValue(float value, AxisBase axis) {
//                return constitutions[(int) value];
//            }
//
//            // we don't draw numbers, so no decimal digits needed
////            @Override
////            public int getDecimalDigits() {  return 0; }
//        };
//
//        xAxis.setGranularity(1f);
//        xAxis.setValueFormatter(formatter);
//
//        chartBar.getAxisRight().setEnabled(false);
//
//        YAxis axisLeft = chartBar.getAxisLeft();
//        axisLeft.setAxisLineWidth(2f);
//        axisLeft.setAxisLineColor(R.color.primarycolor);
//        axisLeft.setStartAtZero(true);
//        axisLeft.setAxisMaxValue(100);
//        axisLeft.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
//
//        chartBar.getLegend().setEnabled(false);
//        chartBar.setData(data);//装载数据
//        chartBar.setFitBars(true); //X轴自适应所有柱形图
//        chartBar.setDragEnabled(false);
//        chartBar.setActivated(false);
//        chartBar.setScaleEnabled(false);
//        chartBar.setDrawGridBackground(false);
//        chartBar.setDrawBorders(false);
//        chartBar.setDescription(null);
//        chartBar.setAlpha(1.0f);
//        chartBar.invalidate();//刷新
//    }

    private void initTop() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick(R.id.btn_retext)
    public void onClick() {
        startActivity(new Intent(this, ConstitutionActivity.class));
    }
}
